import config from '../config.js';
import md5 from './md5.js';

const wxRequest = (params, url) => {
    let header = {
        Accept: 'application/json',
        'Content-Type': 'application/json',
    };
    let timestamp = parseInt(new Date().getTime() / 1000);
    let appid = config.appid;
    let key = config.appkey;
    // console.log(`${url}${timestamp}${key}`);
    let sign = md5.hexMD5(`${url}${timestamp}${key}`);
    header['Sign'] = sign // 关键所在
    header['AppId'] = appid // 关键所在
    header['Timestamp'] = timestamp // 关键所在
    let token = wx.getStorageSync('token');
    if (token) {
        header['Token'] = token // 关键所在
    }
    // console.log(header);
    url = config.base_url + url;
    //设置默认的请求方式
    params.method = params.method || 'GET';
    wx.request({
        url,
        method: params.method,
        data: params.method === 'GET' ? params.data : JSON.stringify(params.data),
        header: header,
        success(res) {
            if (params.success) {
                if (res.code == 401) {
                    wx.showToast({
                        title: '登录认证失败',
                        icon: 'error',
                    })
                }
                params.success(res);
            }
        },
        fail(res) {
            if (params.fail) {
                params.fail(res);
            }
        },
        complete(res) {
            if (params.complete) {
                params.complete(res);
            }
        },
    });
};

const wxUplaodFile = (params, url) => {
  let header = {
      Accept: 'application/json',
      'Content-Type': 'application/json',
  };
  let timestamp = parseInt(new Date().getTime() / 1000);
  let appid = config.appid;
  let key = config.appkey;
  // console.log(`${url}${timestamp}${key}`);
  let sign = md5.hexMD5(`${appid}${url}${timestamp}${key}`);
  header['Sign'] = sign // 关键所在
  header['AppId'] = appid // 关键所在
  header['Timestamp'] = timestamp // 关键所在
  let token = wx.getStorageSync('token');
  if (token) {
      header['Token'] = token // 关键所在
  }
  // console.log(header);
  url = config.base_url + url;
 
  wx.uploadFile({
    url,//服务器端接收图片的路径
    filePath: params.filePath,
    name: params.name,
    header:header,
    formData:params.formData,
    success(res) {
        if (params.success) {
            if (res.code == 401) {
                wx.showToast({
                    title: '登录认证失败',
                    icon: 'error',
                })
            }
            params.success(res);
        }
    },
    fail(res) {
        if (params.fail) {
            params.fail(res);
        }
    },
    complete(res) {
        if (params.complete) {
            params.complete(res);
        }
    },
  })

};


//login
const loginWx = (params) => {
  wxRequest(params, `/app/user/loginWxMini`);
}; 
//支付测试
const pay = (params) => {
  wxRequest(params, `/app/pay/mini`);
};  
const getThemeProjectData = (params) => {
  wxRequest(params, `/app/orgWx/themeProject/get`);
};
//获取公共的参数
function getRouter() {
    setTimeout(function(){ 
        var pages = getCurrentPages() //获取加载的页面  
        var currentPage = pages[pages.length - 1] //获取当前页面的对象  
        var url = currentPage.route //当前页面url  
        wx.setStorageSync('Router', `${url}`)
        var options = currentPage.options //如果要获取url中所带的参数可以查看options  
        // console.info("----登录返回页面参数---");
        // console.info(options);
        //参数多时通过&拼接url的参数  
        if (options) {
            var urlWithArgs = url + '?'
            for (var key in options) {
                var value = options[key]
                urlWithArgs += key + '=' + value + '&'
            }
            urlWithArgs = urlWithArgs.substring(0, urlWithArgs.length - 1)
                // wx.setStorageSync('Url', `/${urlWithArgs}`)  
            wx.setStorageSync('Router', `${urlWithArgs}`)
        }
    },500);
}

module.exports = {
    getRouter,
    loginWx,
    pay,
    // getDestinationList,
    // generateFace,
    // orderSave,
    // orderPay,
    // updateProfile,
    // activtyOrderSave,
    // getThemeProjectData
};